﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BillGuru
{
    public partial class FrmStockViewer : Form
    {
        private BillGuru.Persistence.Product productPersistenceHandler;
        private BillGuru.UI.ProductGridViewController productGridViewController;

        public FrmStockViewer()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'billGuruDataSet.Product' table. You can move, or remove it, as needed.
            productPersistenceHandler = new BillGuru.Persistence.Product(this.productTableAdapter, this.billGuruDataSet);
            //productPersistenceHandler.Fill();

            productGridViewController = new BillGuru.UI.ProductGridViewController(this.productDataGridView);


        }

        private void productDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
        }

        private void pbSave_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.productBindingSource.EndEdit();
            this.productPersistenceHandler.saveChanges();
            //int count = this.tableAdapterManager.UpdateAll(this.billGuruDataSet);
            //MessageBox.Show(count.ToString());

        }

        private void pbMultiClone_Click(object sender, EventArgs e)
        {
            productGridViewController.MultiClone();
        }

        private void pbMaster_Click(object sender, EventArgs e)
        {
            using (FrmStockMaster master = new FrmStockMaster())
            {
                master.ShowDialog();

                BillGuru.BillGuruDataSet.ProductDataTable newProducts = master.NewProducts;
                if (newProducts == null)
                    return;
                billGuruDataSet.Product.Merge(newProducts);
            }
        }

        private void pbDone_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pbShow_Click(object sender, EventArgs e)
        {
            productPersistenceHandler.FillByDescription(tbFilter.Text);
        }

        private void FrmStockViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.billGuruDataSet.HasChanges())
                BillGuru.UI.PermissionHandler.checkDataLoss(e);
        }

    }
}
